package com.cxy.controller;

import com.cxy.pojo.Emp;
import com.cxy.pojo.PageBean;
import com.cxy.pojo.Result;
import com.cxy.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/emps")
public class EmpController {

    @Autowired
    private EmpService empService;
//    /**
//     * 员工分页查询
//     * param:
//     *  pageSize：代表每页呈现的记录数目
//     *  page：分页查询的页码，默认为1
//     */
//    @GetMapping
//    public Result limitSelect(@RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize, @RequestParam(value = "page", defaultValue = "1") Integer page){
//        log.info("员工分页查询");
//
//        PageBean pageBean = empService.limitPageHelperSelect(pageSize,page);
//
//        return Result.success(pageBean);
//    }

    @GetMapping
    public Result limitIfSelect(@RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                @RequestParam(value = "page", defaultValue = "1") Integer page,
                                String name, Short gender,
                                @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin, @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end){
        log.info("分页选择查询：{},{},{},{},{},{}",pageSize,page,name,gender,begin,end);

        PageBean pageBean = empService.limitIfSelect(pageSize,page,name,gender,begin,end);

        return Result.success(pageBean);

    }

    @DeleteMapping("/{ids}")
    public Result deleteIds(@PathVariable List<Integer> ids){
        log.info("批量删除：{}",ids);
        empService.delete(ids);
        return Result.success();
    }

    @PostMapping
    public Result insertEmp(@RequestBody Emp emp){
        log.info("插入一个新员工：{}",emp);

        empService.insert(emp);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getEmpById(@PathVariable Integer id){
        log.info("根据id查找员工信息并回显到前端，id:{}",id);
        Emp emp = empService.getById(id);

        return Result.success(emp);
    }

    @PutMapping
    public Result updateEmp(@RequestBody Emp emp){
        log.info("更新员工信息：{}" ,emp);
        empService.updateEmp(emp);
        return Result.success();
    }
}
